package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.file.service.FileStorageService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.model.wemedia.pojos.WmNewsMaterial;
import com.heima.utils.threadlocal.WmThreadLocalUtil;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.mapper.WmNewsMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
@Slf4j
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {

    @Autowired
    private FileStorageService fileStorageService;

    @Autowired
    private WmNewsMaterialMapper wmNewsMaterialMapper;

    @Autowired
    private WmMaterialMapper wmMaterialMapper;
    /**
     * 素材上传
     *
     * @param file
     * @return
     */
    @Override
    @Transactional
    public ResponseResult upload(MultipartFile file) {
        String prefix = UUID.randomUUID().toString();
        String originalFilename = file.getOriginalFilename();
        //1.上传图片到minio中
        try {
            String string = fileStorageService.uploadImgFile(prefix, originalFilename, new ByteArrayInputStream(file.getBytes()));
            WmMaterial wmMaterial = new WmMaterial();
            wmMaterial.setCreatedTime(new Date());
            wmMaterial.setUrl(string);
            wmMaterial.setType((short) 0);
            wmMaterial.setIsCollection((short) 0);
            wmMaterial.setUserId(WmThreadLocalUtil.getUser().getId());
            wmMaterialMapper.insert(wmMaterial);
            log.info("图片上传成功，上传地址为：{}",string);
            return ResponseResult.okResult(wmMaterial);
        } catch (IOException e) {
            return ResponseResult.errorResult(501,"文件上传参数失效");
        }
        //TODO
    }

    /**
     * 查询素材列表
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult list(WmMaterialDto dto) {
        Page<WmMaterial> page = this.lambdaQuery()
                .eq(dto.getIsCollection() != 0, WmMaterial::getIsCollection, dto.getIsCollection())
                .page(new Page<>(dto.getPage(), dto.getSize()));
        PageResponseResult pageResponseResult = new PageResponseResult();
        pageResponseResult.setCurrentPage(dto.getPage());
        pageResponseResult.setSize(dto.getSize());
        pageResponseResult.setTotal((int) page.getTotal());
        pageResponseResult.setData(page.getRecords());
        //TODO
        return pageResponseResult;
    }

    /**
     * 删除素材
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult del(Integer id) {
        //TODO
        QueryWrapper<WmNewsMaterial> query = Wrappers.<WmNewsMaterial>query().eq("material_id", id);
        List<WmNewsMaterial> wmNewsMaterialList = wmNewsMaterialMapper.selectList(query);
        if((wmNewsMaterialList!=null && wmNewsMaterialList.size()!= 0)){
            return ResponseResult.errorResult(501,"文件删除失败");
        }
        WmMaterial wmMaterial = wmMaterialMapper.selectById(id);
        fileStorageService.delete(wmMaterial.getUrl());

        wmMaterialMapper.deleteById(wmMaterial.getId());
        return ResponseResult.okResult("操作成功");
    }

    /**
     * 取消收藏
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult cancel(Integer id) {
        WmMaterial wmMaterial = wmMaterialMapper.selectById(id);
        wmMaterial.setIsCollection((short) 0);
        wmMaterialMapper.updateById(wmMaterial);
        //TODO
        return ResponseResult.okResult("操作成功");
    }

    /**
     * 收藏
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult collect(Integer id) {
        //TODO
        WmMaterial wmMaterial = wmMaterialMapper.selectById(id);
        wmMaterial.setIsCollection((short) 1);
        wmMaterialMapper.updateById(wmMaterial);
        //TODO
        return ResponseResult.okResult("操作成功");
    }
}
